package com.yzlwb.skt.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yzlwb.common.core.openapi.OpenApiConstant;
import com.yzlwb.common.utils.DateUtils;
import com.yzlwb.common.utils.http.HttpClientUtil;
import com.yzlwb.skt.domain.SktProject;
import com.yzlwb.skt.mapper.SktProjectMapper;
import com.yzlwb.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yzlwb.skt.mapper.SktWaylineMapper;
import com.yzlwb.skt.domain.SktWayline;
import com.yzlwb.skt.service.ISktWaylineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 航线Service业务层处理
 * 
 * @author wfy
 * @date 2025-11-11
 */
@Service
public class SktWaylineServiceImpl implements ISktWaylineService 
{
    @Autowired
    private SktWaylineMapper sktWaylineMapper;

    @Autowired
    private SktProjectMapper sktProjectMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询航线
     * 
     * @param id 航线主键
     * @return 航线
     */
    @Override
    public SktWayline selectSktWaylineById(Long id)
    {
        return sktWaylineMapper.selectSktWaylineById(id);
    }

    /**
     * 查询航线列表
     * 
     * @param sktWayline 航线
     * @return 航线
     */
    @Override
    public List<SktWayline> selectSktWaylineList(SktWayline sktWayline)
    {
        return sktWaylineMapper.selectSktWaylineList(sktWayline);
    }

    /**
     * 新增航线
     * 
     * @param sktWayline 航线
     * @return 结果
     */
    @Override
    public int insertSktWayline(SktWayline sktWayline)
    {
        sktWayline.setCreateTime(DateUtils.getNowDate());
        return sktWaylineMapper.insertSktWayline(sktWayline);
    }

    /**
     * 修改航线
     * 
     * @param sktWayline 航线
     * @return 结果
     */
    @Override
    public int updateSktWayline(SktWayline sktWayline)
    {
        sktWayline.setUpdateTime(DateUtils.getNowDate());
        return sktWaylineMapper.updateSktWayline(sktWayline);
    }

    /**
     * 批量删除航线
     * 
     * @param ids 需要删除的航线主键
     * @return 结果
     */
    @Override
    public int deleteSktWaylineByIds(Long[] ids)
    {
        return sktWaylineMapper.deleteSktWaylineByIds(ids);
    }

    /**
     * 删除航线信息
     * 
     * @param id 航线主键
     * @return 结果
     */
    @Override
    public int deleteSktWaylineById(Long id)
    {
        return sktWaylineMapper.deleteSktWaylineById(id);
    }

    @Override
    @Transactional
    public int getSaveWayline(String twoUserToken ,Long deptId, String username) {

        sktWaylineMapper.deleteWaylineByDeptId(deptId);

        SktProject sktProject = new SktProject();
        sktProject.setDeptId(deptId);
        List<SktProject> sktProjectList = sktProjectMapper.selectSktProjectList(sktProject);
        for (SktProject sp: sktProjectList){

            Map<String,Object> headerMap = new HashMap<String,Object>();
            headerMap.put("X-User-Token",twoUserToken);
            headerMap.put("X-Project-Uuid",sp.getTwoProjectUuid());

            String resultStr = HttpClientUtil.doGet(OpenApiConstant.projectWaylineList,headerMap);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer code = resultJson.getInteger("code");
            if(0==code) {
                JSONArray jsonArray = resultJson.getJSONObject("data").getJSONArray("list");
                if(jsonArray!=null && !jsonArray.isEmpty()){
                    for(int i=0;i<jsonArray.size();i++){
                        JSONObject js = jsonArray.getJSONObject(i);
                        SktWayline sktWayline = new SktWayline();
                        sktWayline.setCreateBy(username);
                        sktWayline.setUpdateBy(username);
                        sktWayline.setCreateTime(new Date(js.getLong("update_time")));
                        sktWayline.setUpdateTime(new Date(js.getLong("update_time")));
                        sktWayline.setTwoProjectUuid(sp.getTwoProjectUuid());
                        sktWayline.setTwoProjectName(sp.getTwoName());
                        sktWayline.setDeptId(deptId);
                        sktWayline.setTwoWaylineId(js.getString("id"));
                        sktWayline.setTwoWaylineName(js.getString("name"));
                        JSONArray payloadInformationArray = js.getJSONArray("payload_information");
                        if(payloadInformationArray!=null && !payloadInformationArray.isEmpty()){
                            sktWayline.setPayloadInformationDomin(payloadInformationArray.getJSONObject(0).getString("domain"));
                            sktWayline.setPayloadInformationType(payloadInformationArray.getJSONObject(0).getString("type"));
                            sktWayline.setPayloadInformationLensType(payloadInformationArray.getJSONObject(0).getString("lens_type"));
                        }
                        sktWayline.setDeviceModelKey(js.getString("device_model_key"));
                        sktWayline.setTemplateTypes(js.getJSONArray("template_types").getString(0));
                        String infoStr = HttpClientUtil.doGet(OpenApiConstant.projectWaylineList+"/"+js.getString("id"),headerMap);
                        JSONObject infoJson = JSONObject.parseObject(infoStr);
                        Integer infoCode = infoJson.getInteger("code");
                        if(0==infoCode) {
                            JSONObject infoJb = infoJson.getJSONObject("data");
                            sktWayline.setTwoDistance(infoJb.get("distance")+"");
                            sktWayline.setTwoDownloadUrl(infoJb.getString("download_url"));
                            sktWayline.setWaylinePointNums(infoJb.getInteger("wayline_point_nums")+"");
                        }
                        sktWaylineMapper.insertSktWayline(sktWayline);
                    }
                }
            }

        }
        return 1;
    }
}
